<template>
  <section>
    <!--工具条：关键字查询 + 新增-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0;">
      <el-form :inline="true">
        <el-form-item>
          <el-input v-model="keyword" placeholder="关键字"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="getUsers">关键字</el-button>
        </el-form-item>
<!--        <el-form-item>
          &lt;!&ndash;v-perm="'menu:add'"&ndash;&gt;
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>-->
      </el-form>
    </el-col>

    <!--列表：优秀实践，copy domain修改列表-->
    <el-table :data="users" highlight-current-row v-loading="listLoading" @selection-change="selsChange"
              style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
<!--      <el-table-column type="index" width="60">
      </el-table-column>-->
      <el-table-column prop="headImg" label="头像"  sortable>
        <template slot-scope="scope">
          <img :src="scope.row.headImg" width="50px" height="50px">
        </template>
      </el-table-column>
      <el-table-column prop="username" label="姓名"  sortable>
      </el-table-column>
      <el-table-column prop="phone" label="电话" sortable>
      </el-table-column>
      <el-table-column prop="email" label="邮箱"  sortable>
      </el-table-column>
      <el-table-column prop="age" label="年龄" sortable>
      </el-table-column>
      <el-table-column prop="state" label="状态"  sortable>
        <template slot-scope="scope">
          <span v-if="scope.row.state==0" style="color: red">禁用</span>
          <span v-else style="color: green">启用</span>
        </template>
      </el-table-column>
      <el-table-column prop="createtime" label="创建时间" sortable>
      </el-table-column>

      <el-table-column label="操作" min-width="180">
        <template slot-scope="scope">
          <el-button  size="small" :disabled="scope.row.state === 1" @click="enable(scope.row)">启用</el-button>
          <el-button  size="small" :disabled="scope.row.state === 0" @click="disable(scope.row)">禁用</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :current-page="currentPage"
                     :page-size="pageSize" :total="totals" style="float:right;">
      </el-pagination>
    </el-col>


  </section>
</template>

<script>

export default {
  data() {
    return {
      //分页显示所属数据
      users: [],
      listLoading: false,
      currentPage: 1,
      pageSize: 10,
      totals: 0,

      //高级查询所需数据
      keyword: '',

      //多条数据选中
      sels: [],//列表选中列

    }
  },
  methods: {
    disable(row){
      this.$confirm('确定禁用该用户吗?', '提示', {
      }).then(()=>{
        this.$http.put("/user/disable/" + row.id).then(res => {
          let {success} = res.data;
          if (success) {
            //刷新界面
            this.getUsers();
          } else {
            this.$message({
              message: '删除失败',
              type: 'error'
            });
          }
        })
      })
    },
    enable(row){
      this.$confirm('确定启用该用户吗?', '提示', {
      }).then(()=>{
        this.$http.put("/user/enable/" + row.id).then(res => {
          let {success} = res.data;
          if (success) {
            //刷新界面
            this.getUsers();
          } else {
            this.$message({
              message: '删除失败',
              type: 'error'
            });
          }
        })
      })
    },
    //获取菜单列表
    getUsers() {
      let paras = {
        currentPage: this.currentPage,
        pageSize: this.pageSize,
        keyword: this.keyword
      };
      console.log(paras);
      this.listLoading = true;
      this.$http.post("/user/selectByQueryPage", paras).then(res => {
        console.log(res);
        this.totals = res.data.total;
        this.users = res.data.list;
        this.listLoading = false;
      }).catch(res => {
        this.$message.error("获取分页数据失败!!!");
        this.listLoading = true;
      })
    },

    //分页查询 - 点击某一页查询
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getUsers();
    },

    //点击排序
    handleChange(value) {
      this.saveForm.index = value;
    },

    //删除
    handleDel: function (index, row) {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        this.$http.delete("/user/delete/" + row.id).then(res => {
          this.listLoading = false;
          if (res.data.success) {
            //计算总页数
            let totalPage = this.totals % this.pageSize == 0 ? this.totals / this.pageSize
                : Math.ceil(this.totals / this.pageSize);
            if (this.currentPage > 1 && this.currentPage == totalPage && (this.totals - 1) % this.pageSize == 0) {
              this.currentPage = this.currentPage - 1;
            }
            this.getUsers();
          } else {
            this.$message.error("删除失败【500】");
          }
        }).catch(res => {
          this.$message.error("系统错误【400,404】");
        })
      }).catch(() => {
        //确认框点击取消之后走这里
      });
    },

    selsChange: function (sels) {
      this.sels = sels;
    },

    //批量删除
    batchRemove: function () {
      var ids = this.sels.map(item => item.id);
      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        this.$http.patch("/user/patchDelete", ids).then(res => {
          this.listLoading = false;
          if (res.data.success) {
            this.currentPage = 1;
            this.getUsers();
          } else {
            this.$message.error("批量删除失败【500】");
          }
        }).catch(res => {
          this.$message.error("系统错误【400,404】");
        })
      }).catch(() => {
      });
    },
  },
  mounted() {
    //分页查询 - 页面一加载查询
    this.getUsers();
  }
}

</script>

<style scoped>

</style>